import React from 'react';
import { Modal, Descriptions, Tag, Typography, Divider, Table, Empty, Button } from 'antd';
import { RecruitmentAnnouncement, PlanType, ApproveStatus } from '@/lib/recruitment';
import dayjs from 'dayjs';

const { Title, Paragraph } = Typography;

interface AnnouncementDetailModalProps {
  open: boolean;
  onClose: () => void;
  announcement: RecruitmentAnnouncement | null;
}

const AnnouncementDetailModal: React.FC<AnnouncementDetailModalProps> = ({
  open,
  onClose,
  announcement
}) => {
  if (!announcement) return null;

  const getPlanTypeTag = (type: number) => {
    switch (type) {
      case PlanType.MANAGEMENT:
        return <Tag color="blue">管理岗位</Tag>;
      case PlanType.TEACHER:
        return <Tag color="green">专任教师</Tag>;
      case PlanType.OTHER:
        return <Tag color="orange">其他岗位</Tag>;
      default:
        return <Tag>未知类型</Tag>;
    }
  };

  const getStatusTag = (status: number) => {
    switch (status) {
      case ApproveStatus.PENDING_PUBLISH:
        return <Tag color="warning">发布未审批</Tag>;
      case ApproveStatus.PENDING_MODIFY:
        return <Tag color="processing">修改未审批</Tag>;
      case ApproveStatus.APPROVED:
        return <Tag color="success">已通过</Tag>;
      case ApproveStatus.REJECTED:
        return <Tag color="error">已拒绝</Tag>;
      default:
        return <Tag>未知状态</Tag>;
    }
  };

  // 渲染用人部门和岗位信息
  const renderDepartments = () => {
    if (!announcement?.depts?.length) {
      return <Empty description="暂无用人部门信息" />;
    }

    return announcement.depts.map((dept, deptIndex) => (
      <div key={deptIndex} className="mb-4">
        <Title level={5}>用人部门: {dept.numbering}</Title>
        <Table
          size="small"
          pagination={false}
          dataSource={dept.positions}
          columns={[
            {
              title: '岗位名称',
              dataIndex: 'positionName',
              key: 'positionName',
            },
            {
              title: '岗位编号',
              dataIndex: 'positionNumber',
              key: 'positionNumber',
            },
            {
              title: '岗位性质',
              dataIndex: 'positionProperties',
              key: 'positionProperties',
              render: (text: string | null) => text || '-',
            },
            {
              title: '岗位描述',
              dataIndex: 'jobDescription',
              key: 'jobDescription',
            },
            {
              title: '任职要求',
              dataIndex: 'jobRequired',
              key: 'jobRequired',
            },
            {
              title: '招聘人数',
              dataIndex: 'person',
              key: 'person',
            },
            {
              title: '专业技能权重',
              dataIndex: 'professionalSkillsRight',
              key: 'professionalSkillsRight',
              render: (value: number) => `${value}%`,
            },
            {
              title: '沟通能力权重',
              dataIndex: 'communicationSkillsRight',
              key: 'communicationSkillsRight',
              render: (value: number) => `${value}%`,
            },
            {
              title: '团队协作权重',
              dataIndex: 'teamworkAbilityRight',
              key: 'teamworkAbilityRight',
              render: (value: number) => `${value}%`,
            },
          ]}
        />
      </div>
    ));
  };

  return (
    <Modal
      title="招聘公告详情"
      open={open}
      onCancel={onClose}
      width={800}
      footer={null}
    >
      <Title level={4}>{announcement.planName}</Title>
      <Descriptions column={2}>
        <Descriptions.Item label="计划编号">{announcement.planCode}</Descriptions.Item>
        <Descriptions.Item label="计划类型">{getPlanTypeTag(announcement.planType)}</Descriptions.Item>
        <Descriptions.Item label="发布日期">{dayjs(announcement.publishDate).format('YYYY-MM-DD')}</Descriptions.Item>
        <Descriptions.Item label="审批状态">{getStatusTag(announcement.approveStatus)}</Descriptions.Item>
        <Descriptions.Item label="开始日期">{dayjs(announcement.startDate).format('YYYY-MM-DD')}</Descriptions.Item>
        <Descriptions.Item label="结束日期">{dayjs(announcement.endDate).format('YYYY-MM-DD')}</Descriptions.Item>
      </Descriptions>

      {announcement.recruiterId && (
        <>
          <Divider orientation="left">招聘负责人信息</Divider>
          <Descriptions column={2}>
            <Descriptions.Item label="负责人姓名">{announcement.recruiterName}</Descriptions.Item>
            <Descriptions.Item label="联系电话">{announcement.recruiterPhone}</Descriptions.Item>
          </Descriptions>
        </>
      )}

      {/* 用人部门和岗位信息 */}
      {announcement.planType !== PlanType.OTHER && (
        <>
          <Divider orientation="left">用人部门及岗位信息</Divider>
          {renderDepartments()}
        </>
      )}

      {announcement.recruitmentConditions && (
        <>
          <Divider orientation="left">招聘条件</Divider>
          <Paragraph>{announcement.recruitmentConditions}</Paragraph>
        </>
      )}

      {announcement.benefitsDescription && (
        <>
          <Divider orientation="left">待遇说明</Divider>
          <Paragraph>{announcement.benefitsDescription}</Paragraph>
        </>
      )}

      {announcement.interviewProcess && (
        <>
          <Divider orientation="left">面试流程</Divider>
          <Paragraph>{announcement.interviewProcess}</Paragraph>
        </>
      )}

      {announcement.content && (
        <>
          <Divider orientation="left">其他说明</Divider>
          <Paragraph>{announcement.content}</Paragraph>
        </>
      )}

      {announcement.notes && (
        <>
          <Divider orientation="left">备注</Divider>
          <Paragraph>{announcement.notes}</Paragraph>
        </>
      )}
    </Modal>
  );
};

export default AnnouncementDetailModal; 